<?php

include 'functions/func.php';

class Vevent {

    var $dateDebut;
    var $dateFin;
    var $nom;
    var $description;
	var $sequence;
	var $iud;
	var $alarms = array();
}

class Valarm {
    var $date;
    var $nom;
    var $description;
}

$nbAlarms = 0;

global $event1;
global $alarm1;

$eventsTab = array();

$nbEvents = 0;

global $calendar_name;



		$ifile = @fopen($filename, 'r');
		if ($ifile == FALSE) exit(error($lang['l_error_cantopen'], $filename));
		$nextline = fgets($ifile, 1024);
		if (trim($nextline) != 'BEGIN:VCALENDAR') exit(error($lang['l_error_invalidcal'], $filename));

		
		while (!feof($ifile)) {
			$line = $nextline;
			$nextline = fgets($ifile, 1024);
			//echo $nextline."<br/>";
			while (isset($nextline{0}) && ($nextline{0} == ' ' || $nextline{0} == "\t")) {
				$line = $line . substr($nextline, 1);
				$nextline = fgets($ifile, 1024);
				$nextline = ereg_replace("[\r\n]", '', $nextline);
			}
			$line = str_replace('\n', "\n", $line);
			$line = str_replace('\t', "\t", $line);
			$line = trim(stripslashes($line));
			switch ($line) {
				// Begin VFREEBUSY/VEVENT Parsing
				//
				case 'BEGIN:VFREEBUSY':
				case 'BEGIN:VEVENT':
					$event1 = new Vevent();
					$nbEvents += 1;
					$valarm_set = FALSE;
					break;
				case 'END:VFREEBUSY':
				case 'END:VEVENT':
					$eventsTab[$nbEvents-1] = $event1;
					break;

				// Begin VTODO Parsing
				//
				case 'END:VTODO':
					break;

				case 'BEGIN:VTODO':
					break;

				// Begin VALARM Parsing
				//
				case 'BEGIN:VALARM':
					$valarm_set = TRUE;
					$alarm1 = new Valarm();
					$nbAlarms += 1;
					break;
				case 'END:VALARM':
					$valarm_set = FALSE;
					$event1->alarms[$nbAlarms-1] = $alarm1;
					break;

				default:
					if (true){  
					preg_match('/([^:]+):(.*)/', $line, $matches);
					$field = $matches['1'];
					$data = $matches['2'];
					$property = strtoupper($field);
					$prop_pos = strpos($property,';');
					if ($prop_pos !== false) $property = substr($property,0,$prop_pos);

					switch ($property) {
						// Start VTODO Parsing
						//
						case 'DUE':
							break;

						case 'COMPLETED':
							break;

						case 'PRIORITY':
							break;

						case 'STATUS':
							break;

						case 'GEO':
							break;

						case 'CLASS':
							break;

						case 'CATEGORIES':
							break;
						
						case 'DTSTART':
							$datetime = extractDateTime($data, $property, $field);
							$event1->dateDebut = $datetime;
							break;

						case 'DTEND':
							$datetime = extractDateTime($data, $property, $field);
							$event1->dateFin = $datetime;
							break;

						case 'EXDATE':
							$data = split(',', $data);
							foreach ($data as $exdata) {
								$exdata = str_replace('T', '', $exdata);
								$exdata = str_replace('Z', '', $exdata);
								preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})/', $exdata, $regs);
								$except_dates[] = $regs[1] . $regs[2] . $regs[3];
								// Added for Evolution, since they dont think they need to tell me which time to exclude.
								if ($regs[4] == '' && isset($start_time) && $start_time != '') {
									$except_times[] = $start_time;
								} else {
									$except_times[] = $regs[4] . $regs[5];
								}
							}
							break;

						case 'SUMMARY':
							$data = str_replace('$', '&#36;', $data);
							$data = stripslashes($data);
							if ($valarm_set == FALSE) {
								$event1->nom = $data;
							} else {
								$alarm1->nom = $data;
							}
							break;

						case 'DESCRIPTION':
							$data = str_replace('$', '&#36;', $data);
							$data = stripslashes($data);
							$data = htmlentities(urlencode($data));
							if ($valarm_set == FALSE) {
								$event1->description = $data;
							} else {
								$alarm1->description = $data;
							}
							break;

						case 'RECURRENCE-ID':
							break;

						case 'SEQUENCE':
							$event1->sequence = $data;
							break;
						case 'UID':
							$event1->uid = $data;
							break;
						case 'X-WR-CALNAME':
							$calendar_name = $data;
							break;
						case 'X-WR-TIMEZONE':
							break;
						case 'DURATION':
							break;
						case 'RRULE':
							break;
						case 'ATTENDEE':
							break;
						case 'ORGANIZER':
							break;
						case 'LOCATION':
							break;
						case 'URL':
							break;
						default:
							if(strpos(':',$data) > 1) $other .= $data;
					}
				}
			}
		}
		
		echo "<h1> Calendrier : ".$calendar_name."</h1>";
		
		foreach($eventsTab as $event){
			echo "<div class=\"event\">";
			echo "<p>".$event->nom."</p>";
			echo "<p>".date("F j, Y, g:i a" , mktime(0, 0, 0
									, $event->dateDebut["jour"]
									, $event->dateDebut["mois"]
									, $event->dateDebut["annee"]))."</p>";
			echo "<p>".date("F j, Y, g:i a" , mktime(0, 0, 0
									, $event->dateFin["jour"]
									, $event->dateFin["mois"]
									, $event->dateFin["annee"]))."</p>";
			$i=0;
			$nbA = count($event->alarms);
			if($nbA != 0){
				echo "<p>+".$nbA." alarm(s)</p>";
			}
			/*while ($i < $nbA){
				$alarm = $event->alarms[$i];
				echo "<p>".$alarm->nom."</p>";
			}*/
			echo "</div>";
		}
		
		fclose($ifile);

?>